﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlexiCommerce.Components;
using System.Web.DomainServices;

namespace FlexiCommerce.Web
{
    public class FlexiDomainService : System.Web.DomainServices.DomainService
    {
        #region "Language"
        [Query]
        public IQueryable<Language> AllLanguages()
        {
            return Find.Languages;
        }

        [Insert]
        public void InsertLanguage(Language language)
        {
            Context.Persister.Save(language);
        }

        [Delete]
        public void DeleteLanguage(Language language)
        {
            Context.Persister.Delete(language);
        }

        [Update]
        public void UpdateLanguage(Language newLanguage, Language originalLanguage)
        {
            Context.Persister.Save(newLanguage);
        }
        #endregion

        #region "Category"
        [Query]
        public IQueryable<Category> AllCategories()
        {
            return Find.Categories;
        }

        [Insert]
        public void InsertCategory(Category category)
        {
            Context.Persister.Save(category);
        }

        [Delete]
        public void DeleteCategory(Category category)
        {
            Context.Persister.Delete(category);
        }

        [Update]
        public void UpdateCategory(Category newCategory, Category originalCategory)
        {
            Context.Persister.Save(newCategory);
        }
        #endregion

        #region "Category Details"
        [Query]
        public IQueryable<CategoryDetails> AllCategoryDetails()
        {
            return Find.CategoryDetails;
        }

        [Insert]
        public void InsertCategoryDetail(CategoryDetails categoryDetail)
        {
            Context.Persister.Save(categoryDetail);
        }

        [Delete]
        public void DeleteCategoryDetail(CategoryDetails categoryDetail)
        {
            Context.Persister.Delete(categoryDetail);
        }

        [Update]
        public void UpdateCategoryDetail(CategoryDetails newCategoryDetail, CategoryDetails originalCategoryDetail)
        {
            CategoryDetails detail = Find.CategoryDetails.Single(c => c.ID == newCategoryDetail.ID);
            detail.Name = newCategoryDetail.Name;
            detail.Description = newCategoryDetail.Description;
            Context.Persister.Save(detail);
        }
        #endregion

        #region "Product"
        [Query]
        public IQueryable<Product> AllProducts()
        {
            return Find.Products;
        }

        [Insert]
        public void InsertProduct(Product product)
        {
            Context.Persister.Save(product);
        }

        [Delete]
        public void DeleteProduct(Product product)
        {
            Context.Persister.Delete(product);
        }

        [Update]
        public void UpdateProduct(Product newProduct, Product originalProduct)
        {
            Context.Persister.Save(newProduct);
        }
        #endregion

        #region "Category Details"
        [Query]
        public IQueryable<ProductDetails> AllProductDetails()
        {
            return Find.ProductDetails;
        }

        [Insert]
        public void InsertProductDetail(ProductDetails productDetail)
        {
            Context.Persister.Save(productDetail);
        }

        [Delete]
        public void DeleteProductDetail(ProductDetails productDetail)
        {
            Context.Persister.Delete(productDetail);
        }

        [Update]
        public void UpdateProductDetail(ProductDetails newProductDetail, ProductDetails originalProductDetail)
        {
            ProductDetails detail = Find.ProductDetails.Single(c => c.ID == newProductDetail.ID);
            detail.Name = newProductDetail.Name;
            Context.Persister.Save(detail);
        }
        #endregion

        #region "Configuration"
        [Query]
        public IQueryable<Components.Configuration> AllConfigurations()
        {
            IList<Components.Configuration> config = new List<Components.Configuration>();
            config.Add(Context.Persister.GetConfiguration());
            return config.AsQueryable<Components.Configuration>();
        }

        [Update]
        public void UpdateConfiguration(Components.Configuration newConfiguration, Components.Configuration originalConfiguration)
        {
            Components.Configuration config = Find.Configurations.Single(c => c.ID == newConfiguration.ID);
            config.Store.ThumbnailHeight = newConfiguration.Store.ThumbnailHeight;
            config.Store.ThumbnailWidth = newConfiguration.Store.ThumbnailWidth;
            config.Store.TinyHeight = newConfiguration.Store.TinyHeight;
            config.Store.TinyWidth = newConfiguration.Store.TinyWidth;
            Context.Persister.Save(config);
        }
        #endregion
        
    }
}
